﻿using System;

namespace UnityEngine.Rendering.Universal
{

    public enum GlitchDirection
    {
        Horizontal = 0,
        Vertical = 1,
        HorizontalVertical = 2,
    }

    [Serializable]
    public sealed class GlitchRGBSplitDirectionParameter : VolumeParameter<GlitchDirection>
    {
        public GlitchDirection m_GlitchDirection;

        public GlitchRGBSplitDirectionParameter(GlitchDirection glitchDirection, bool overrideState = false) : base(
            glitchDirection, overrideState)
        {
            m_GlitchDirection = glitchDirection;
        }
    }

    public class GlitchRGBSplit : VolumeComponent, IPostProcessComponent
    {
        public BoolParameter ActiveOn = new BoolParameter(false, true);

        public GlitchRGBSplitDirectionParameter splitDirection =
            new GlitchRGBSplitDirectionParameter(GlitchDirection.Horizontal);

        public ClampedFloatParameter fading = new ClampedFloatParameter(1f, 0.0f, 1.0f);
        public ClampedFloatParameter amount = new ClampedFloatParameter(1.0f, 0.0f, 5.0f);
        public ClampedFloatParameter speed = new ClampedFloatParameter(1.0f, 0.0f, 10.0f);
        public ClampedFloatParameter centerFading = new ClampedFloatParameter(1.0f, 0.0f, 1.0f);
        public ClampedFloatParameter amountR = new ClampedFloatParameter(1.0f, 0.0f, 5.0f);
        public ClampedFloatParameter amountB = new ClampedFloatParameter(1.0f, 0.0f, 5.0f);
        
        static readonly int m_ParamsId = Shader.PropertyToID("_Params");
        static readonly int m_ParamsId2 = Shader.PropertyToID("_Params2");

        private float _timeX = 1.0f;
        
        public bool IsActive()
        {
            return active && ActiveOn.value;
        }

        public bool IsTileCompatible()
        {
            return false;
        }

        public override void SetActive(bool value)
        {
            base.SetActive(value);
            ActiveOn.overrideState = true;
            ActiveOn.value = true;
        }

        public override void UpdateVector3(float f1, float f2, float f3, int type = 0)
        {
            fading.overrideState = true;
            fading.value = f1;

            speed.overrideState = true;
            speed.value = f2;

            amount.overrideState = true;
            amount.value = f3;

            splitDirection.overrideState = true;
            splitDirection.value = (GlitchDirection)type;
        }

        public void Render(CommandBuffer cmd, Material material, RTHandle source, RTHandle destination)
        {
            _timeX += Time.deltaTime;
            if (_timeX > 100)
                _timeX = 0.0f;
            
            material.SetVector(m_ParamsId, new Vector4(fading.value, amount.value, speed.value, centerFading.value));
            material.SetVector(m_ParamsId2, new Vector3(_timeX, amountB.value, amountR.value));
            cmd.Blit(source, BlitDstDiscardContend(cmd, destination), material, (int)splitDirection.value);
        }
    }
}